package com.controller;

import com.util.TxtFileFilter;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SaveAction implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {

        if (InitialThread.resultTextArea.getText().length() == 0) {
            InitialThread.logger.debug("result为空,不可保存");
        } else {
            InitialThread.logger.debug("result可以保存, 请选择");
            InitialThread.logger.debug("内容为{}", InitialThread.resultTextArea.getText());

            JFileChooser fc = new JFileChooser();
            fc.addChoosableFileFilter(new TxtFileFilter());
            fc.setAcceptAllFileFilterUsed(false);

            String filename = new SimpleDateFormat("hh时mm分ss秒-yyyy年MM月dd日").format(new Date()) + ".txt";
            fc.setSelectedFile(new File("./" + filename));

            int returnVal = fc.showSaveDialog(null);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                InitialThread.logger.debug("{}", fc.getSelectedFile());

                Path saveFile = fc.getSelectedFile().toPath();
                try (BufferedWriter out = Files.newBufferedWriter(saveFile, StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW)) {
                    String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
                    out.append("******** ").append(date).append(" ********");
                    out.newLine();

                    if (InitialThread.addrScanJRadio1.isSelected()) {
                        out.append("目标主机(IP地址扫描): ");
                        for (int i = 0; i < InitialThread.getIpAddrTextField().length; i++) {
                            out.append(InitialThread.getIpAddrTextField()[i].getText());
                            if (i < 3)
                                out.append(".");
                            else if (i == 3)
                                out.append("-");
                            else
                                out.newLine();
                        }
                    } else {
                        out.append("目标主机(域名扫描): ").append(InitialThread.domainNameTextField.getText());
                        out.newLine();
                    }

                    String[] log = InitialThread.resultTextArea.getText().split("\\n");
                    for (String s : log) {
                        out.append(s);
                        out.newLine();
                    }

                    out.flush();
                } catch (FileAlreadyExistsException e1) {
                    JOptionPane.showMessageDialog(fc, "文件名已存在", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                } catch (IOException e1) {
                    InitialThread.logger.error("文件保存出现未知异常");
                    e1.printStackTrace();
                }

                JOptionPane.showMessageDialog(fc, "保存完成", "信息", JOptionPane.INFORMATION_MESSAGE);
            }

        }
    }
}
